package com.ruoyi.zhmk.service.impl;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.carApi.entity.CarIdentification;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zhmk.domain.ZhmkCarDevice;
import com.ruoyi.zhmk.mapper.ZhmkCarDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkCarinoutInfoMapper;
import com.ruoyi.zhmk.domain.ZhmkCarinoutInfo;
import com.ruoyi.zhmk.service.IZhmkCarinoutInfoService;

/**
 * 车辆进出记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-24
 */
@Service
public class ZhmkCarinoutInfoServiceImpl implements IZhmkCarinoutInfoService 
{
    @Autowired
    private ZhmkCarinoutInfoMapper zhmkCarinoutInfoMapper;
    @Autowired
    private ZhmkCarDeviceMapper zhmkCarDeviceMapper;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询车辆进出记录
     * 
     * @param id 车辆进出记录ID
     * @return 车辆进出记录
     */
    @Override
    public ZhmkCarinoutInfo selectZhmkCarinoutInfoById(Long id)
    {
        return zhmkCarinoutInfoMapper.selectZhmkCarinoutInfoById(id);
    }

    /**
     * 查询车辆进出记录列表
     * 
     * @param zhmkCarinoutInfo 车辆进出记录
     * @return 车辆进出记录
     */
    @Override
    public List<ZhmkCarinoutInfo> selectZhmkCarinoutInfoList(ZhmkCarinoutInfo zhmkCarinoutInfo)
    {
        return zhmkCarinoutInfoMapper.selectZhmkCarinoutInfoList(zhmkCarinoutInfo);
    }

    /**
     * 新增车辆进出记录
     * 
     * @param zhmkCarinoutInfo 车辆进出记录
     * @return 结果
     */
    @Override
    public int insertZhmkCarinoutInfo(ZhmkCarinoutInfo zhmkCarinoutInfo)
    {
        zhmkCarinoutInfo.setCreateTime(DateUtils.getNowDate());
        return zhmkCarinoutInfoMapper.insertZhmkCarinoutInfo(zhmkCarinoutInfo);
    }

    /**
     * 修改车辆进出记录
     * 
     * @param zhmkCarinoutInfo 车辆进出记录
     * @return 结果
     */
    @Override
    public int updateZhmkCarinoutInfo(ZhmkCarinoutInfo zhmkCarinoutInfo)
    {
        zhmkCarinoutInfo.setUpdateTime(DateUtils.getNowDate());
        return zhmkCarinoutInfoMapper.updateZhmkCarinoutInfo(zhmkCarinoutInfo);
    }

    /**
     * 批量删除车辆进出记录
     * 
     * @param ids 需要删除的车辆进出记录ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarinoutInfoByIds(Long[] ids)
    {
        return zhmkCarinoutInfoMapper.deleteZhmkCarinoutInfoByIds(ids);
    }

    /**
     * 删除车辆进出记录信息
     * 
     * @param id 车辆进出记录ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarinoutInfoById(Long id)
    {
        return zhmkCarinoutInfoMapper.deleteZhmkCarinoutInfoById(id);
    }

    /**
     * 保存车辆记录
     * @param carIdentification
     * @return
     */
    @Override
    public int saveCarInfo(CarIdentification carIdentification) {
        String filePath = RuoYiConfig.getUploadPath();
        String fileName = System.currentTimeMillis()+"";
        String dateStr = simpleDateFormat.format(new Date());
        String filePathname = filePath+"/car/"+dateStr+"/"+fileName+".jpg";
        File file = new File(filePath+"/car/"+dateStr+"/");
        if(!file.exists()){
            file.mkdirs();
        }
        String webUrl = "/profile/upload/car/"+dateStr+"/"+fileName+".jpg";
        //保存车辆图片
        byte[] panoramaImageData = carIdentification.getPanoramaImageData();

        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(filePathname));
            out.write(panoramaImageData);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //根据ip查询出设备信息
        String ip = carIdentification.getIp();
        ZhmkCarDevice zhmkCar = zhmkCarDeviceMapper.selectZhmkCarDeviceByIp(ip);
        //进出类型
        Integer inoutType = zhmkCar.getInoutType();
        //保存车辆进出记录表
        ZhmkCarinoutInfo zhmkCarinout = new ZhmkCarinoutInfo();
        zhmkCarinout.setPlateNumber(carIdentification.getPlateNumber());
        zhmkCarinout.setInoutType(inoutType);
        zhmkCarinout.setCarIp(ip);
        zhmkCarinout.setAvatar(webUrl);
        zhmkCarinout.setCreateTime(DateUtils.getNowDate());
        zhmkCarinout.setCarType(carIdentification.getCarType());
        zhmkCarinout.setOutdoorSite(zhmkCar.getDevicePosition());
        zhmkCarinoutInfoMapper.insertZhmkCarinoutInfo(zhmkCarinout);
        return 1;
    }
}
